package account.view;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import tools.old.files.MainView;
import tools.old.files.TeacherFrameExt;
import account.controller.InloggenController;
import account.docent.view.DocentView;
import account.model.SubTeam;
import account.model.InloggenModel;

import java.awt.Font;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.SystemColor;
import java.awt.Toolkit;

public class InloggenView extends JPanel implements KeyListener  {

	private static InloggenView instance;

	public static JPanel mainPanel = new JPanel();
	public static JFrame frame = new JFrame();
	public JButton btnInloggen = new JButton("Inloggen");
	public JTextField gebruikersnaamTF;
	public JPasswordField wachtwoordTF;

	private MailRecoveryView mrv = new MailRecoveryView();
	private InloggenController ic = new InloggenController();
	private InloggenView iv;

	private final JLabel lblWachtwoordVergeten = new JLabel("Wachtwoord vergeten?");
	private final JMenuBar menuBar = new JMenuBar();
	private final JMenu mnNewMenu = new JMenu("Start");
	private final JMenu mnNewMenu_1 = new JMenu("Account");
	private final JMenuItem mntmWachtwoord = new JMenuItem("Wachtwoord vergeten");
	private final JMenuItem mntmAfsluiten = new JMenuItem("Afsluiten");

	public static InloggenView getInstance() {
		if (instance == null) {
			instance = new InloggenView();
		}
		return instance;
	}

	private InloggenView() {
		FrameSetup();
	}

	private void FrameSetup() {
		//buttons
		btnInloggen.addActionListener(new InloggenController(btnInloggen));
		btnInloggen.setActionCommand("Inloggen");
		
		//framesetup
		JLabel lblGebruikersnaam = new JLabel("Gebruikersnaam");
		lblGebruikersnaam.setForeground(Color.BLACK);
		lblGebruikersnaam.setFont(new Font("Tahoma", Font.PLAIN, 13));

		gebruikersnaamTF = new JTextField();
		gebruikersnaamTF.setColumns(10);
		frame.getContentPane().setBackground(SystemColor.menu);
		frame.setLocationRelativeTo(null);

		JLabel lblWachtwoord = new JLabel("Wachtwoord");
		lblWachtwoord.setForeground(Color.BLACK);
		lblWachtwoord.setFont(new Font("Tahoma", Font.PLAIN, 13));

		setWachtwoordTF(new JPasswordField());
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(mainPanel, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
								.addGroup(groupLayout.createSequentialGroup()
										.addGap(47)
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
												.addComponent(lblGebruikersnaam)
												.addComponent(lblWachtwoord))
												.addGap(18)
												.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
														.addComponent(wachtwoordTF)
														.addComponent(gebruikersnaamTF, GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
														.addComponent(btnInloggen, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
														.addComponent(lblWachtwoordVergeten))))
														.addContainerGap(79, Short.MAX_VALUE))
				);

		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addComponent(mainPanel, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
						.addGap(64)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(gebruikersnaamTF, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblGebruikersnaam))
								.addGap(18)
								.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(wachtwoordTF, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblWachtwoord))
										.addPreferredGap(ComponentPlacement.RELATED)
										.addComponent(lblWachtwoordVergeten)
										.addGap(7)
										.addComponent(btnInloggen, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
										.addContainerGap(119, Short.MAX_VALUE))
				);
		btnInloggen.setFont(new Font("Tahoma", Font.BOLD, 11));
		frame.getContentPane().setLayout(groupLayout);
		frame.setResizable(false);
		frame.setTitle("Inloggen");
		frame.setSize(360, 320);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setJMenuBar(menuBar);

		menuBar.add(mnNewMenu);
		mnNewMenu.add(mntmAfsluiten);
		menuBar.add(mnNewMenu_1);
		mnNewMenu_1.add(mntmWachtwoord);

		mntmWachtwoord.addActionListener(new InloggenController(mntmWachtwoord));
		mntmWachtwoord.setActionCommand("Wachtwoord");

		mntmAfsluiten.addActionListener(new InloggenController(mntmAfsluiten));
		mntmAfsluiten.setActionCommand("Afsluiten");

		//wachtwoord vergeten
		lblWachtwoordVergeten.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				mrv.run();
			}
		});

		//mogelijkheid om met entertoets in te loggen
		gebruikersnaamTF.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {ic.LoginButton();}						
		});
		wachtwoordTF.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {ic.LoginButton();}						
		});
	}

	public JTextField getGebruikersnaamTF() {
		return gebruikersnaamTF;
	}

	public void setGebruikersnaamTF(JTextField gebruikersnaamTF) {
		this.gebruikersnaamTF = gebruikersnaamTF;
	}

	public void setWachtwoordTF(JPasswordField wachtwoordTF) {
		this.wachtwoordTF = wachtwoordTF;
	}

	public JPasswordField getWachtwoordTF() {
		return wachtwoordTF;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		e.consume();
	}

	@Override
	public void keyReleased(KeyEvent e) {
		e.consume();
	}

	@Override
	public void keyTyped(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER){
		}
	}
}
